function getOrder(){
	$.ajax({
		type:"post",
		url:"getOrder",
		success:function(result){
			/*设置标题*/
			$(".personal-eq-tab h2").text("欢迎你，"+result.resultParm.user.username);
			/*设置器材租借表格*/
			for(var i=0;i<result.resultParm.orders.length;i++){
				if(result.resultParm.orders[i].status==0){
					var newLine = 
						"<tr>"+
							"<td>"+result.resultParm.orders[i].id+"</td>"+
							"<td>"+result.resultParm.orders[i].equipment.name+"</td>"+
							"<td>"+result.resultParm.orders[i].number+"</td>"+
							"<td>未审核</td>"+
							"<td>"+"<a title="+"\"delete-eq\""+" href="+"\'#\'"+">删除</a>"+"</td>"+
							"<td></td>"+
						"</tr>";
					$(".personal-eq table").append(newLine);
				}else if(result.resultParm.orders[i].status==1){
					if(result.resultParm.orders[i].isReturn==0 && result.resultParm.orders[i].applyReturn==0){
						var newLine = 
							"<tr>"+
								"<td>"+result.resultParm.orders[i].id+"</td>"+
								"<td>"+result.resultParm.orders[i].equipment.name+"</td>"+
								"<td>"+result.resultParm.orders[i].number+"</td>"+
								"<td>已审核</td>"+
								"<td></td>"+
								"<td>"+"<a title="+"\"return-eq\""+" href="+"\'#\'"+">归还</a>"+"</td>"+
							"</tr>";
						$(".personal-eq table").append(newLine);
					}else if(result.resultParm.orders[i].isReturn==0 && result.resultParm.orders[i].applyReturn==1){
						var newLine = 
							"<tr>"+
								"<td>"+result.resultParm.orders[i].id+"</td>"+
								"<td>"+result.resultParm.orders[i].equipment.name+"</td>"+
								"<td>"+result.resultParm.orders[i].number+"</td>"+
								"<td>已审核</td>"+
								"<td></td>"+
								"<td>审核中</td>"+
							"</tr>";
						$(".personal-eq table").append(newLine);
					}else if(result.resultParm.orders[i].isReturn==1){
						var newLine = 
							"<tr>"+
								"<td>"+result.resultParm.orders[i].id+"</td>"+
								"<td>"+result.resultParm.orders[i].equipment.name+"</td>"+
								"<td>"+result.resultParm.orders[i].number+"</td>"+
								"<td>已审核</td>"+
								"<td></td>"+
								"<td>已归还</td>"+
							"</tr>";
						$(".personal-eq table").append(newLine);
					}
					
				}
			};
			delete_eq();
			return_eq();
		},
	});
}


function delete_eq(){
	$("a[title=delete-eq]").click(function(){
		$.ajax({
			type:"post",
			url:"deleteOrder",
			data:{
				id:$(this).parent().parent().children().eq(0).text(),
			},
			success:function(result){
				alert(result.resultInfo);
				$(".personal-eq table tr td").remove();
				getOrder();
			},
			error:function(){
				alert("删除失败");
			}
			
		});
	});
};
function return_eq(){
	$("a[title=return-eq]").click(function(){
		$.ajax({
			type:"post",
			url:"applyForReturn",
			data:{
				id:$(this).parent().parent().children().eq(0).text(),
			},
			success:function(result){
				alert(result.resultInfo);
				$(".personal-eq table tr td").remove();
				getOrder();
			},
			error:function(){
				alert("删除失败");
			}
			
		});
	});
};




/*获得场地信息*/
function getPlaceOrders(){
				$.ajax({
				type:"get",
				url:"findAllByAccount",
//				data:{
//					id:account，
//				},
				success:function(result){
					for(var i=0;i<result.resultParm.places.length;i++){
						if(result.resultParm.places[i].status==0){
							var newLine = 
								"<tr>"+
									"<td>"+result.resultParm.places[i].placeOrderId+"</td>"+
									"<td>"+result.resultParm.places[i].place.placeName+"</td>"+
									"<td>"+result.resultParm.places[i].place.placeId+"</td>"+
									"<td>"+result.resultParm.places[i].orderTime+"</td>"+
									"<td>审核中</td>"+
									"<td>"+"<a title="+"\"delete-place\""+" href="+"\'#\'"+">删除</a>"+"</td>"+
								"</tr>";
							$(".personal-place table").append(newLine);
						}else if(result.resultParm.places[i].status==1){
							var newLine = 
								"<tr>"+
									"<td>"+result.resultParm.places[i].placeOrderId+"</td>"+
									"<td>"+result.resultParm.places[i].place.placeName+"</td>"+
									"<td>"+result.resultParm.places[i].place.placeId+"</td>"+
									"<td>"+result.resultParm.places[i].orderTime+"</td>"+
									"<td>已审核</td>"+
									"<td></td>"+
								"</tr>";
							$(".personal-place table").append(newLine);
						}else if(result.resultParm.places[i].status==2){
							var newLine = 
								"<tr>"+
									"<td>"+result.resultParm.places[i].placeOrderId+"</td>"+
									"<td>"+result.resultParm.places[i].place.placeName+"</td>"+
									"<td>"+result.resultParm.places[i].place.placeId+"</td>"+
									"<td>"+result.resultParm.places[i].orderTime+"</td>"+
									"<td>拒绝申请</td>"+
									"<td></td>"+
								"</tr>";
							$(".personal-place table").append(newLine);
						}else if(result.resultParm.places[i].status==3){
							var newLine = 
								"<tr>"+
									"<td>"+result.resultParm.places[i].placeOrderId+"</td>"+
									"<td>"+result.resultParm.places[i].place.placeName+"</td>"+
									"<td>"+result.resultParm.places[i].place.placeId+"</td>"+
									"<td>"+result.resultParm.places[i].orderTime+"</td>"+
									"<td>已完成</td>"+
									"<td></td>"+
								"</tr>";
							$(".personal-place table").append(newLine);
						}
					}
					delete_place();
				},
				
			});
};


/*删除场地*/
function delete_place(){
	$("a[title=delete-place]").click(function(){
//		alert($(this).parent().parent().children().eq(0).text());
		$.ajax({
			type:"get",
			url:"deletePlace",
			data:{
				placeId:$(this).parent().parent().children().eq(0).text(),
			},
			success:function(result){
				alert(result.resultInfo);
				$(".personal-place table tr td").remove();
				getPlaceOrders();
			},
			error:function(){
				alert("删除失败");
			}
		});
	});
};
/*获得场地信息*/
getPlaceOrders();
/*获得器材信息*/
getOrder();




/*赛事模块个人中心*/
function getMatch(){
	$.ajax({
		type:"get",
		url:"findMatches",
		success:function(result){
//			console.log(result.resultParm.places.length);
//			for(var i=0;i<result.resultParm.places.length;i++){
//				if(result.resultParm.places.status){
//					
//				}
//				
//			};
			var info = result.resultParm.places;
			$.each(info, function(index , item) {
				
//					console.log(item.places==null);
					var gameId = $("<td></td>").append(item.id);
					var gameName = $("<td></td>").append(item.matchName);
					var startTime = $("<td></td>").append(item.startTime);
					var endTime = $("<td></td>").append(item.endTime);
					var placeA = $("<td></td>").append(item.places==null?"无":$("<a></a>").append("详情").attr("href","#placeDetails").attr("data-toggle","modal").attr("title","placeA"));
					var eqA = $("<td></td>").append(item.orders==null?"无":$("<a></a>").append("详情").attr("href","#eqDetails").attr("data-toggle","modal").attr("title","eqA"));
					var deleteA = $("<td></td>").append(item.status==0 ?$("<a></a>").append("删除").attr("href","#details").attr("title","deleteA"):"  ");
					var status = $("<td></td>").append(item.status==0 ?"未审核":"审核通过 ");
					$("<tr></tr>")
					.append(gameId)
					.append(gameName)
					.append(startTime)
					.append(endTime)
					.append(placeA)
					.append(eqA)
					.append(deleteA)
					.append(status)
					.appendTo(".personal-game table");
			});
			placeA();
			eqA();
			deleteA();
		},
	});
};
/*场地详情超链接*/
function placeA(){
	$("a[title=placeA]").click(function(){
		
		var num1 = $(this).parent().parent().children().eq(0).text();
		$.ajax({
			type:"get",
			url:"findMatches",
			success:function(result){
				var info = result.resultParm.places;
				$.each(info, function(index , item){
					if(num1==item.id){
						var placeId = item.place.placeId;
						var placeName =item.place.placeName;
						var placeMoney =item.place.placeMoney;
						$("#placeId").val(placeId);
						$("#placeName").val(placeName);
						$("#placeMoney").val(placeMoney);
					};	
				});
			},
		});
	});
};
/*器材详情超链接*/
function eqA(){
	$("a[title=eqA]").click(function(){
		var num1 = $(this).parent().parent().children().eq(0).text();
		$.ajax({
			type:"get",
			url:"findMatches",
			success:function(result){
				var info = result.resultParm.places;
				$.each(info, function(index , item){
//					console.log(item.equipment);
					if(num1==item.id){
						var eqId = item.equipment.id;
						var eqName =item.equipment.name;
						var eqMoney =item.equipment.remainNum;
						$("#eqId").val(eqId);
						$("#eqName").val(eqName);
						$("#eqNum").val(eqMoney);
					};	
				});
			},
		});
	});
};
/*删除按钮*/
function deleteA(){
	$("a[title=deleteA]").click(function(){
		var num1 = $(this).parent().parent().children().eq(0).text();
		alert(num1);
		$.ajax({
			type:"get",
			url:"deleteMatch",
			data:{
				matchId :num1,
			},
			success:function(result){
				alert(result.resultInfo);
				
			},
			error:function(){
				alert("删除失败");
				$("table tr td").remove();
				getMatch();
			}
		});
	});
	
}

/*进入页面获得赛事*/
getMatch();


/*修改密码*/
$("#changPasswordBtn").click(function(){
	$.ajax({
		url:"changePassword",
		data:{
			oldPassowrd:$("#oldPassword").val(),
			password:$("#changePassword").val(),
		},
		type:"POST",
		success:function(result){
			if(result.resultParm.code == "200"){
				alert(result.resultInfo);
			} else {
				alert(result.resultInfo);
				$("#changePasswordModal").modal("hide");
				alert("请重新登录!");
				window.location.href="../index.html";
			}
		},
		error:function(){
			alert("修改密码失败");
		}
	});
});

